package com.zkthink.ceres.applet.homePage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.homePage.domain.vo.FindCategoryListPageVo;
import com.zkthink.ceres.applet.homePage.service.HomePageService;
import com.zkthink.ceres.applet.product.domain.entity.CeresProductCategory;
import com.zkthink.ceres.applet.product.mapper.CeresProductCategoryMapper;
import com.zkthink.ceres.applet.server.domain.entity.PcServerCategory;
import com.zkthink.ceres.applet.server.mapper.PcServerCategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author cmz
 * @Date 2024-05-08 10:30
 */
@Service
public class HomePageServiceImpl implements HomePageService {
    @Resource
    private PcServerCategoryMapper pcServerCategoryMapper;
    
    @Resource
    private CeresProductCategoryMapper ceresProductCategoryMapper;
    
    @Override
    public R findHomePageCategory() {
        //服务分类
        Page<PcServerCategory> pcServerCategoryPage = new Page<>(1,4);
        QueryWrapper<PcServerCategory> pcServerCategoryQueryWrapper = new QueryWrapper<>();
        pcServerCategoryQueryWrapper.eq("is_delete","0");
        pcServerCategoryQueryWrapper.eq("shelves","1");
        List<PcServerCategory> pcServerCategoryList = pcServerCategoryMapper.selectPage(pcServerCategoryPage, pcServerCategoryQueryWrapper).getRecords();
        //产品分类
        Page<CeresProductCategory> ceresProductCategoryPage = new Page<>(1, 4);
        QueryWrapper<CeresProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("is_delete","0");
        productCategoryQueryWrapper.eq("status","1");
        List<CeresProductCategory> ceresProductCategoryList = ceresProductCategoryMapper.selectPage(ceresProductCategoryPage, productCategoryQueryWrapper).getRecords();

        List<Map<String, Object>> list = new ArrayList<>();
        
        //服务类型列表处理
        Map<String, Object> serverMap = new HashMap<>();
        serverMap.put("title","服务分类");
        ArrayList<HashMap<String, Object>> serverList = new ArrayList<>();
        for (PcServerCategory pcServerCategory : pcServerCategoryList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title","服务分类");
            map.put("id",pcServerCategory.getId());
            map.put("serverName",pcServerCategory.getServerName());
            map.put("serverImg",pcServerCategory.getServerImg());
            map.put("brief",pcServerCategory.getBrief());

            serverList.add(map);
        }
        serverMap.put("data",serverList);
        list.add(serverMap);
        
        
        //产品类型列表处理
        Map<String, Object> productMap = new HashMap<>();
        productMap.put("title","产品分类");
        ArrayList<HashMap<String, Object>> productList = new ArrayList<>();
        for (CeresProductCategory ceresProductCategory : ceresProductCategoryList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title","产品分类");
            map.put("id",ceresProductCategory.getId());
            map.put("serverName",ceresProductCategory.getCategoryName());
            map.put("serverImg",ceresProductCategory.getCategoryImg());
            map.put("brief",ceresProductCategory.getShortName());

            productList.add(map);
        }
        productMap.put("data",productList);
        list.add(productMap);
        return R.success(list);
    }

    @Override
    public R findHomeProductCategory() {
        //产品分类
        QueryWrapper<CeresProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryQueryWrapper.eq("is_delete","0");
        productCategoryQueryWrapper.eq("status","1");
        productCategoryQueryWrapper.eq("is_course","0");
        List<CeresProductCategory> ceresProductCategoryList = ceresProductCategoryMapper.selectList(productCategoryQueryWrapper);
        //产品类型列表处理
        ArrayList<ArrayList<HashMap<String, Object>>> list = new ArrayList<>();
        ArrayList<HashMap<String, Object>> productList = new ArrayList<>();
        ArrayList<HashMap<String, Object>> productListTwo = new ArrayList<>();
        int i = 1;
        for (CeresProductCategory ceresProductCategory : ceresProductCategoryList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title","产品分类");
            map.put("id",ceresProductCategory.getId());
            map.put("serverName",ceresProductCategory.getCategoryName());
            map.put("serverImg",ceresProductCategory.getCategoryImg());
            map.put("brief",ceresProductCategory.getShortName());
            if (i%2 == 0){
                productListTwo.add(map);
            }else{
                productList.add(map);
            }
            i++;
        }
        list.add(productList);
        list.add(productListTwo);
        return R.success(list);
    }

    @Override
    public R findCategoryList(FindCategoryListPageVo findCategoryListPageVo) {
        
        if (findCategoryListPageVo.getKeyword().equals("服务分类")){
            //服务分类数据
            Page<PcServerCategory> page = new Page<>(findCategoryListPageVo.getPageNum(),findCategoryListPageVo.getPageSize());
            QueryWrapper<PcServerCategory> pcServerCategoryQueryWrapper = new QueryWrapper<>();
            pcServerCategoryQueryWrapper.eq("is_delete","0");
            pcServerCategoryQueryWrapper.eq("shelves","1");
            Page<PcServerCategory> pcServerCategoryPage = pcServerCategoryMapper.selectPage(page, pcServerCategoryQueryWrapper);
            List<PcServerCategory> records = pcServerCategoryPage.getRecords();
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (PcServerCategory record : records) {
                Map<String, Object> map = new HashMap<>();
                map.put("title","服务分类");
                map.put("id",record.getId());
                map.put("serverName",record.getServerName());
                map.put("serverImg",record.getServerImg());
                map.put("brief",record.getBrief());
                maps.add(map);
            }
            Page page1 = new Page();
            page1.setRecords(maps);
            page1.setTotal(pcServerCategoryPage.getTotal());
            page1.setSize(pcServerCategoryPage.getSize());
            page1.setCurrent(pcServerCategoryPage.getCurrent());
            page1.setOrders(pcServerCategoryPage.getOrders());
            page1.setPages(pcServerCategoryPage.getPages());
            return R.success(page1);
        }
        else {
            //产品分类数据
            Page<CeresProductCategory> page = new Page<>(findCategoryListPageVo.getPageNum(),findCategoryListPageVo.getPageSize());
            QueryWrapper<CeresProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
            productCategoryQueryWrapper.eq("status","1");
            productCategoryQueryWrapper.eq("is_delete","0");
            productCategoryQueryWrapper.eq("is_course","0");
            Page<CeresProductCategory> ceresProductCategoryPage = ceresProductCategoryMapper.selectPage(page, productCategoryQueryWrapper);
            List<CeresProductCategory> records = ceresProductCategoryPage.getRecords();

            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (CeresProductCategory record : records) {
                Map<String, Object> map = new HashMap<>();
                map.put("title","产品分类");
                map.put("id",record.getId());
                map.put("serverName",record.getCategoryName());
                map.put("serverImg",record.getCategoryImg());
                map.put("brief",record.getShortName());
                maps.add(map);
            }
            Page page2 = new Page();
            page2.setRecords(maps);
            page2.setTotal(ceresProductCategoryPage.getTotal());
            page2.setSize(ceresProductCategoryPage.getSize());
            page2.setCurrent(ceresProductCategoryPage.getCurrent());
            page2.setOrders(ceresProductCategoryPage.getOrders());
            page2.setPages(ceresProductCategoryPage.getPages());
            return R.success(page2);
        }
    }


}
